package wei.xinya.pattern.creational.factory.SimpleFactory;

import wei.xinya.pattern.creational.factory.shape.Circle;
import wei.xinya.pattern.creational.factory.shape.Rectangle;
import wei.xinya.pattern.creational.factory.shape.Shape;
import wei.xinya.pattern.creational.factory.shape.Square;

import java.lang.reflect.InvocationTargetException;

class ShapeFactory {

    /**
     * 使用 getShape 方法获取形状类型的对象
     *
     * @param type 形状类型
     * @return Shape
     */
    static Shape getShape(String type) {
        if (type == null) {
            return null;
        } else if (type.equalsIgnoreCase("CIRCLE")) {
            return new Circle();
        } else if (type.equalsIgnoreCase("RECTANGLE")) {
            return new Rectangle();
        } else if (type.equalsIgnoreCase("SQUARE")) {
            return new Square();
        } else {
            return null;
        }
    }

    static Object getClass(Class<? extends Shape> clazz) {
        Object object = null;
        try {
            // 通过反射 newInstance 新建一个实例
            object = Class.forName(clazz.getName()).getDeclaredConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return object;
    }
}
